package com.meet.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.net.URL;


import java.net.URL;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 连接mySQL数据库工具类
 */

public class JDBCutil {

    private static Connection connection = null;

    /**
     * 获取连接
     *
     * @return
     */
    public static Connection getConnection(){

        Properties properties = new Properties();

        try {

            ClassLoader classLoader = JDBCutil.class.getClassLoader();
            URL resource = classLoader.getResource("mysql.properties");
            String path = resource.getPath();
            //String path1 = path.substring(1,path.length());
            properties.load(new FileInputStream(path));



            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");

            Class.forName(driver);
            System.out.println("加载驱动完毕");
            connection = DriverManager.getConnection(url, user, password);
            System.out.println("获取连接");
            System.out.println(connection);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("加载驱动出问题");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取文件失败");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库出错");
        }
        return connection;

    }


    /**
     * 关闭连接
     */
    public static void closeConnection() {
        try {
            connection.close();
            System.out.println("mysql数据库关闭");
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

}
